using System;
using System.Collections.Generic;
using UnityEngine;

namespace Yoozoo.Gameplay.Liberty
{
    /// <summary>
    /// 瞄准点动画控制
    /// </summary>
    public class LockPointAnimation : MonoBehaviour
    {
        // 准星动画
        [SerializeField]
        private Animation animation;

        [SerializeField]
        private string clipName;

        [SerializeField]
        private float speed = 1f;

        [SerializeField]
        private float reverseSpeed = 0.7f;
        
        // 击中效果动画
        [SerializeField]
        private Animation effectAnimation;
        
        [SerializeField]
        private string effectClipName;
        
        [SerializeField]
        private Color normalColor = Color.white;
        
        [SerializeField]
        private Color hitTargetColor = Color.white;

        [SerializeField]
        private List<Renderer> colorRenderers;

        [SerializeField]
        private GameObject rootObject;

        private MaterialPropertyBlock materialPropertyBlock = null;

        void OnEnable()
        {
            // 重置瞄准状态
            var animationState = animation[clipName];
            animation.Play(clipName);
            
            animationState.time = 0;
            animation.Sample();
            animation.Stop();
        }
        
        void OnDisable()
        {
            // 击中特效
            if (effectAnimation.gameObject.activeSelf)
            {
                effectAnimation.SetActive(false);
            }
        }

        /// <summary>
        /// 设置准星是否可见
        /// </summary>
        /// <param name="active"></param>
        public void SetVisible(bool active)
        {
            if (rootObject.activeSelf != active)
            {
                rootObject.SetActive(active);

                if (active)
                {
                    // 重置瞄准状态
                    var animationState = animation[clipName];
                    animation.Play(clipName);
            
                    animationState.time = 0;
                    animation.Sample();
                    animation.Stop();
                }
            }
        }

        public void PlayAttackAnimation(bool hitTarget, bool isDead)
        {
            var animationState = animation[clipName];
            animationState.speed = speed;
            animation.Play(clipName);

            // 击中特效
            if (effectAnimation)
            {
                if (hitTarget)
                {
                    if (!effectAnimation.gameObject.activeSelf)
                    {
                        effectAnimation.SetActive(true);
                    }
                    effectAnimation.Play(effectClipName);
                    SetColor(isDead ? hitTargetColor : normalColor);
                }
                else
                {
                    if (!effectAnimation.gameObject.activeSelf)
                    {
                        if (effectAnimation.gameObject.activeSelf)
                        {
                            effectAnimation.SetActive(false);
                        }
                    }
                }
            }
        }

        public void PlayAttackAnimationReverse()
        {
            var animationState = animation[clipName];
            animationState.speed = -reverseSpeed;
            animation.Play(clipName);
        }

        public void SetColor(Color color)
        {
            if (colorRenderers==null || colorRenderers.Count==0)
            {
                return;
            }
            
            if (materialPropertyBlock == null)
            {
                materialPropertyBlock = new MaterialPropertyBlock();
            }
            
            materialPropertyBlock.Clear();
            materialPropertyBlock.SetColor("_Color", color);
            for (int i = 0; i < colorRenderers.Count; ++i)
            {
                colorRenderers[i].SetPropertyBlock(materialPropertyBlock);
            }
        }

        // 动画播完事件
        void OnAnimationComplete()
        {
            PlayAttackAnimationReverse();
        }
        
    }
}